package com.zzj.hotel.core.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;


public class FileUtil {
	/**
	 * 获取Excel内容到List<String[][]>中
	 * @param path
	 * @return
	 */
	public static String copyFile(String orignPath,String id,HttpServletRequest request){
		Date now = new Date();
		File file = new File(orignPath);
		String fileName = file.getName();
		String fileSuffixName = fileName.substring(fileName.lastIndexOf(".")+1);
		String realpath = request.getSession().getServletContext().getRealPath("/"+uploaFilepath);
		//String realpath = ServletActionContext.getServletContext().getRealPath("/upload") ;//获取服务器路径
		if(isPicture(fileSuffixName)){
			File t = new File(realpath+"\\"+id+"."+fileSuffixName);
			
	        // 复制文件  
	        int byteread = 0; // 读取的字节数  
	        InputStream in = null;  
	        OutputStream out = null;  
	  
	        try {  
	            in = new FileInputStream(file);  
	            out = new FileOutputStream(t);  
	            byte[] buffer = new byte[1024];  
	  
	            while ((byteread = in.read(buffer)) != -1) {  
	                out.write(buffer, 0, byteread);  
	            }  
	        } catch (FileNotFoundException e) {  
	            e.printStackTrace();  
	        } catch (IOException e) {  
	            e.printStackTrace();
	        } finally {  
	            try {  
	                if (out != null)  
	                    out.close();  
	                if (in != null)  
	                    in.close();  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	            }  
	        }
			return id+"."+fileSuffixName;
		}
		return file.getName();
	}
	public static boolean isPicture(String fileSuffixName){
		String pictureSuffixNames = "jpg.png,gif,bmp.jpeg";
		int i = pictureSuffixNames.indexOf(fileSuffixName);
		return pictureSuffixNames.indexOf(fileSuffixName)>=0;
	}
	public static final String uploaFilepath = "view/uploadFile";
	public static void main(String[] args) {
		System.out.println(4%3);
	}
}
